<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .progress-bar{
            width: 600px;
            height: 35px;
            margin: 50px auto;
        }
        .progress-bar .left{
            width: 550px;
            height: 35px;
            background-color: #e5e5e5;
            float: left;
            position: relative;
        }
        .progress-bar .left .bg{
            width: 0;
            height: 35px;
            background-color: red;
        }
        .progress-bar .left .bar{
            width: 60px;
            height: 50px;
            background-color: red;
            position: absolute;
            left: 0;
            top: -7px;
            cursor: move;
        }
        .progress-bar .right{
            width: 50px;
            height: 35px;
            float: left;
            text-align: center;
            line-height: 35px;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="progress-bar">
        <div class="left">
            <div class="bg"></div>
            <div class="bar"></div>
        </div>
        <div class="right">0%</div>
    </div>
    <script src="../js/tools.js"></script>
    <script>
        let box=$(".progress-bar"), bar=$(".bar"), bg=$(".bg"),left=$(".left"),right=$(".right")
        box.onmousedown=function(e){
            //获取鼠标横坐标
            let x=e.offsetX;
           console.log(x);
           console.log(left.offsetWidth);
           console.log(bar.offsetWidth);

            document. onmousemove=function(e){

                let X=e.pageX-bg.parentNode.offsetLeft-x
                if(X<0){
                 X=0
                }else if(X>left.offsetWidth-bar.offsetWidth){
                X=left.style.offsetWidth-bar.style.offsetWidth
                }
                
                bar.style.left=X+"px"
                bg.style.width=X+"px"
                let num=bg.offsetWidth/(left.offsetWidth-bar.offsetWidth)
                right.innerHTML=parseInt(num*100)+"%"
            }
            
        }
        document.onmouseup = function(){
            document.onmousemove = null
        }
    </script>
</body>
</html>